﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.SmartHealthcareZSJ;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.HospitalCommand;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.HospitalCommandHandler
{
    public class HospitalQueryCommandHandler : IRequestHandler<HospitalQueryCommand, APIResult<object>>
    {
        /// <summary>
        /// AutoMapper实例
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// SqlSugar上下文
        /// </summary>
        private readonly SqlSugarContext sugar;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="sugar"></param>
        public HospitalQueryCommandHandler(IMapper mapper, SqlSugarContext sugar)
        {
            this.mapper = mapper;
            this.sugar = sugar;
        }

        /// <summary>
        /// 查询医院信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(HospitalQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                // 构建查询条件
                var query = sugar.db.Queryable<HospitalZ>()
                    .Where(x => x.IsDeleteId == 0) // 只查询未删除的医院
                    .WhereIF(request.HospitalId.HasValue, x => x.HospitalId == request.HospitalId)
                    .WhereIF(!string.IsNullOrEmpty(request.HospitalName), x => x.HospitalName.Contains(request.HospitalName))
                    .WhereIF(request.IsRecommended.HasValue, x => x.IsRecommended == request.IsRecommended)
                    .WhereIF(!string.IsNullOrEmpty(request.Location), x => x.Location.Contains(request.Location));


                // 获取总记录数
                var total = await query.CountAsync();

                // 分页查询
                var list = await query
                    .OrderByDescending(x => x.CreateTime)
                    .Skip((request.PageIndex - 1) * request.PageSize)
                    .Take(request.PageSize)
                    .ToListAsync();

                // 构建返回结果
                var data = new
                {
                    Total = total,
                    List = list
                };

                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = data;
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"查询医院信息时发生异常: {ex.Message}");
                result.Code = APIEnum.查询失败;
                result.Msg = $"查询医院信息失败：{ex.Message}";
                result.data = null;
            }
            return await Task.FromResult(result);
        }
    }
}
